package com.baidu.camera;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.camera.adapter.CameraItemAdapter;
import com.baidu.camera.constants.Keys;
import com.baidu.camera.constants.Tips;
import com.baidu.camera.ui.TextureCameraActivity_;
import com.baidu.camera.ui.SurfaceCameraActivity_;
import com.baidu.camera.ui.USBCamera2Activity_;
import com.baidu.camera.ui.USBCamera3Activity_;
import com.baidu.camera.ui.USBCamera4Activity_;
import com.baidu.camera.ui.USBCameraActivity;
import com.baidu.camera.ui.USBCameraActivity_;
import com.baidu.camera.utils.JumpUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{


    @ViewById
    ListView lv;

    CameraItemAdapter adapter;
    ArrayList<String> data;

    @AfterViews
    void init() {
        data = new ArrayList<>();
        adapter = new CameraItemAdapter(data, this);
        TextView title = new TextView(this);
        title.setBackgroundColor(Color.GRAY);
        title.setText(Tips.MAIN_TITLE);
        title.setGravity(Gravity.CENTER);
        lv.addHeaderView(title);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);

        initData();
    }

    private void initData() {
        data.add(Keys.COMMON_TAKE);
        data.add(Keys.COMMON_TAKE_STORE);
        data.add(Keys.TEXTURE_CAMERA);
        data.add(Keys.SURFACE_CAMERA);
        data.add(Keys.USB_CAMERA);
        data.add(Keys.USB_CAMERA2);
        data.add(Keys.USB_CAMERA3);
        data.add(Keys.USB_CAMERA4);
        adapter.notifyDataSetChanged();
    }



    private void takeStore() {
//        if(){}
        String path = this.getExternalCacheDir().getAbsolutePath();
        File file = new File(path + "/img/");
        if (!file.exists()) {
            file.mkdirs();
        }
        File file2 = new File(file.getAbsolutePath() + "/" + (int) (1000 * Math.random()) + ".png");
        if (!file2.exists()) {
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Uri uri = Uri.fromFile(file2);
        //start Camera
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //store pic in uri
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position==0){
            return;
        }
        switch (data.get(position-1)){
            case Keys.COMMON_TAKE:
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivity(intent);
                break;
            case Keys.COMMON_TAKE_STORE:
                takeStore();
                break;
            case Keys.TEXTURE_CAMERA:
                JumpUtils.simpleJump(this, TextureCameraActivity_.class);
                break;
            case Keys.SURFACE_CAMERA:
                JumpUtils.simpleJump(this, SurfaceCameraActivity_.class);
                break;
            case Keys.USB_CAMERA:
                JumpUtils.simpleJump(this, USBCameraActivity_.class);
                break;
            case Keys.USB_CAMERA2:
                JumpUtils.simpleJump(this, USBCamera2Activity_.class);
                break;
            case Keys.USB_CAMERA3:
                JumpUtils.simpleJump(this, USBCamera3Activity_.class);
                break;
            case Keys.USB_CAMERA4:
                JumpUtils.simpleJump(this, USBCamera4Activity_.class);
                break;
        }
    }
}
